# -*- coding: UTF-8 -*-
"""
:since 2019-03-07
:author liujingjun

之前生成block特征的脚本不知道去哪里了，这次重新写一份，也是copy。

"""
import sys
from datetime import datetime
from datetime import timezone

from pymongo import MongoClient
import numpy as np
from core.utils import Timer
import pandas as pd


class BlockFeatureGen:
    """A class for making feature to mongodb"""

    def __init__(self, ip):
        self.block_db = MongoClient('mongodb://%s:27017/' % ip, connectTimeoutMS=60000 * 60)['bitcoinj_production']

    def make_block_feature(self, end_timestamp, window):
        """
        Read block raw signal from database and convert to feature
        :param window:
        :param end_timestamp:
        :return: block feature
        """
        start_timestamp = end_timestamp - window * 60
        print('Get block feature. Start time: %s, end time: %s' % (start_timestamp, end_timestamp))
        timer = Timer()
        timer.start()
        block_list = self.block_db['block'].find({'time': {'$gt': start_timestamp, '$lte': end_timestamp}})
        block_tx = []
        for block in block_list:
            tx_list = self.block_db['tx'].find({'blockHash': block['_id']})
            for tx in tx_list:
                tx_total_value = self.block_db['output'].aggregate(
                    [{'$match': {'outTxId': tx['_id']}},
                     {'$group': {'_id': None, 'sum': {'$sum': '$value'}}}])
                total_value = float(list(tx_total_value)[0]['sum']) / 100000000
                block_tx.append(total_value)

        timer.stop()
        bins = 10
        hist, bin_edges = np.histogram([tx_value for tx_value in block_tx if tx_value < 100], bins=bins)
        hist = [int(e) for e in hist]
        out_threshold_tx_count = len([tx_value for tx_value in block_tx if tx_value >= 100])
        values = [len(block_tx), round(sum(block_tx), 5)]
        values.extend(hist)
        values.append(out_threshold_tx_count)
        keys = ['block_total_tx_count', 'block_total_value']
        keys.extend(['block-hist-' + str(e) for e in list(range(1, bins + 2, 1))])
        post = dict(zip(keys, values))
        post['_id'] = end_timestamp
        post['time'] = str(datetime.utcfromtimestamp(end_timestamp))
        print(post)
        return post


if __name__ == '__main__':
    """
    例如: python ./BlockFeatureGen.py "2018-12-07 00:00:00" "2018-12-07 00:10:00" 15
    
    # 15 分钟粒度特征
    python ./BlockFeatureGen.py "2018-12-07 00:00:00" "2018-12-15 23:59:00" 15 1 >GenBlock_15_1.log &
    python ./BlockFeatureGen.py "2018-12-16 00:00:00" "2018-12-31 23:59:00" 15 2 >GenBlock_15_2.log &
    
    python ./BlockFeatureGen.py "2019-01-01 00:00:00" "2019-01-15 23:59:00" 15 3 >GenBlock_15_3.log &
    python ./BlockFeatureGen.py "2019-01-16 00:00:00" "2019-01-31 23:59:00" 15 4 >GenBlock_15_4.log &
    
    python ./BlockFeatureGen.py "2019-02-01 00:00:00" "2019-02-15 23:59:00" 15 5 >GenBlock_15_5.log &
    python ./BlockFeatureGen.py "2019-02-16 00:00:00" "2019-02-28 23:59:00" 15 6 >GenBlock_15_6.log &
    
    python ./BlockFeatureGen.py "2019-03-01 00:00:00" "2019-03-06 23:59:00" 15 7 >GenBlock_15_7.log &
    """
    if len(sys.argv) < 4:
        print('参数不对')
        print("例如: python ./BlockFeatureGen.py \"开始时间(utc)\" \"结束时间(utc)\" 窗口大小(15|30|60)")
        exit()

    begin_utctime_str = sys.argv[1]
    end_utctime_str = sys.argv[2]
    window_size = int(sys.argv[3])
    index = sys.argv[4]

    begin_timestamp = int(
        datetime.strptime(begin_utctime_str, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc).timestamp())
    end_timestamp = int(
        datetime.strptime(end_utctime_str, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc).timestamp())
    interval = window_size * 60

    # feature_maker = BlockFeatureGen(ip='39.104.227.148')
    feature_maker = BlockFeatureGen(ip='0.0.0.0')
    block_feature_list = []
    while begin_timestamp <= end_timestamp:
        iter_utctime_str = str(datetime.utcfromtimestamp(begin_timestamp))
        iter_localtime_str = str(datetime.fromtimestamp(begin_timestamp))
        print('iter utc: %s, local: %s' % (iter_utctime_str, iter_localtime_str))
        feature = feature_maker.make_block_feature(int(begin_timestamp), window_size)
        block_feature_list.append(feature)
        print(len(block_feature_list))
        # print(block_feature_list)
        begin_timestamp += 60
        sys.stdout.flush()

    df = pd.DataFrame(block_feature_list)
    df = df.get(['time', 'block_total_tx_count', 'block_total_value', 'block-hist-1', 'block-hist-2', 'block-hist-3',
                 'block-hist-4', 'block-hist-5', 'block-hist-6', 'block-hist-7', 'block-hist-8', 'block-hist-9',
                 'block-hist-10', 'block-hist-11'])
    print('block_feature_%s_%s.csv' % (window_size, index))
    df.to_csv('block_features_%s/block_feature_%s_%s.csv' % (window_size, window_size, index), index=False)
    pass
